package com.acdc.mqtt.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.acdc.mqtt.service.MessageHandler;
import com.acdc.service.DLT698InstructionSender;
import com.acdc.mqtt.entity.HeartUp;
import com.acdc.mqtt.entity.MqttMsg;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.acdc.service.IIotModuleService;
import com.acdc.domain.IotModule;
import com.acdc.domain.IotModuleHeartbeat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.acdc.device.service.IMeterDeviceService;
import com.acdc.device.domain.MeterDevice;

@Slf4j
@Component
public class HeartUpHandler implements MessageHandler {

    @Autowired
    private DLT698InstructionSender dlt698InstructionSender;

    @Autowired
    private IIotModuleService iotModuleService;

    @Autowired
    private IMeterDeviceService meterDeviceService;

    @Override
    public void handleMessage(String topic, String message) {

        String imei = topic.split("\\/")[2];
        log.info("IMIEI: {}, HeartUpHandler: {}", imei, message);
        
        try {
            MqttMsg<HeartUp> mqttMsg = new ObjectMapper().readValue(message, new TypeReference<MqttMsg<HeartUp>>() {});
            HeartUp heartUp = mqttMsg.getData();
            log.info("HeartUp: {}", heartUp);
            IotModule iotModule = iotModuleService.selectIotModuleByImei(imei);
            if (iotModule == null) {
                // 添加新的物联网模块
                iotModule = new IotModule();
                iotModule.setImei(imei);
                iotModuleService.insertIotModule(iotModule);
                log.info("添加新的物联网模块: {}", iotModule);
                // 发送请求ServerAddress指令
                dlt698InstructionSender.queryServerAddress(imei);
            } else {
                // 保存心跳信息
                IotModuleHeartbeat iotModuleHeartbeat = new IotModuleHeartbeat();
                iotModuleHeartbeat.setDfs(heartUp.getDfs());
                iotModuleHeartbeat.setPwrSta(heartUp.getPwr_sta());
                iotModuleHeartbeat.setHeartbeatTime(new Date());
                iotModuleHeartbeat.setIotModuleId(iotModule.getId());
                iotModuleHeartbeat.setIccid(heartUp.getIccid());
                List<IotModuleHeartbeat> iotModuleHeartbeatList = new ArrayList<>();
                iotModuleHeartbeatList.add(iotModuleHeartbeat);
                iotModule.setIotModuleHeartbeatList(iotModuleHeartbeatList);
                iotModuleService.insertIotModuleHeartbeat(iotModule);
                // 更新开关状态
                // iotModule.setPwrSta(heartUp.getPwr_sta());
                // iotModuleService.updateIotModule(iotModule);
                
                // 发起建立连接
                List<MeterDevice> meterDeviceList = meterDeviceService.selectMeterDeviceByIotModuleId(iotModule.getId());
                for (MeterDevice meterDevice : meterDeviceList) {
                    if (meterDevice.getDeviceStatus() == null || meterDevice.getDeviceStatus().equals("1")) {
                        dlt698InstructionSender.sendConnectRequest(imei, meterDevice.getLogicAddr());   
                    } else {
                        if (heartUp.getPwr_sta() == 1) {
                            meterDevice.setDeviceStatus("3");
                        } else {
                            meterDevice.setDeviceStatus("2");
                        }
                        meterDeviceService.updateMeterDevice(meterDevice);
                    }
                }
                if (meterDeviceList.isEmpty()) {
                    dlt698InstructionSender.queryServerAddress(imei);
                }
            }
        } catch (JsonProcessingException e) {
            log.error("HeartUpHandler: {}", e.getMessage());
        }
        
    }
}
